/*
点赞 api 接口
包括文章点赞和一级评论点赞
*/

package v1

import (
    "net/http"
    "strconv"

    "gitee.com/keeving/quanta-test-cat/middleware"
    "gitee.com/keeving/quanta-test-cat/model"
    "gitee.com/keeving/quanta-test-cat/utils/errmsg"
    "github.com/gin-gonic/gin"
)

// CreateLike 新增点赞
func LikeArticle(c *gin.Context) {
    aid := c.Param("aid")
    token := c.GetHeader("Authorization")

    // 获取用户名
    username, err := middleware.GetUserNameFromToken(token)
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "code":  http.StatusBadRequest,
            "msg": err.Error(),
        })
        return
    }

    // 通过用户名获取用户 ID
    id, code := model.GetUserIDByUsername(username)
    if code != errmsg.SUCCESS {
        c.JSON(http.StatusInternalServerError, gin.H{
            "code":  http.StatusInternalServerError,
            "msg": errmsg.GetErrMsg(code),
        })
        return
    }

    // 转换文章 ID
    articleID, err := strconv.Atoi(aid)
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "code":  http.StatusBadRequest,
            "msg": "无效的文章 ID",
        })
        return
    }

    // 切换点赞状态
    code = model.ToggleLike(uint(articleID), id)
    if code == errmsg.SUCCESS {
        c.JSON(http.StatusOK, gin.H{
            "code":  http.StatusOK,
            "msg": "操作成功",
            "data": nil,
        })
    } else {
        c.JSON(http.StatusInternalServerError, gin.H{
            "code":  http.StatusInternalServerError,
            "msg": errmsg.GetErrMsg(code),
        })
    }
}

// 查询用户喜欢文章列表
func GetLike(c *gin.Context) {
    token := c.GetHeader("Authorization")

    // 获取用户名
    username, err := middleware.GetUserNameFromToken(token)
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "code":  http.StatusBadRequest,
            "msg": err.Error(),
        })
        return
    }

    // 通过用户名获取用户 ID
    id, code := model.GetUserIDByUsername(username)
    if code != errmsg.SUCCESS {
        c.JSON(http.StatusInternalServerError, gin.H{
            "code":  http.StatusInternalServerError,
            "msg": errmsg.GetErrMsg(code),
            "test": "1",
        })
        return
    }

    // 查询用户喜欢的文章
    articles, code := model.GetUserLikes(id)
    if code != errmsg.SUCCESS {
        c.JSON(http.StatusInternalServerError, gin.H{
            "status":  http.StatusInternalServerError,
            "message": errmsg.GetErrMsg(code),
            "test": "2",
        })
        return
    }

    // 返回结果
    c.JSON(http.StatusOK, gin.H{
        "code": http.StatusOK,
        "msg": "操作成功",
        "data": gin.H{
            "num": len(articles),
            "articles": articles,
        },
    })
}

// FirstCommentLike 点赞一级评论
func FirstCommentLike(c *gin.Context) {
    cid := c.Param("firstCid")
    token := c.GetHeader("Authorization")

    // 获取用户名
    username, err := middleware.GetUserNameFromToken(token)
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "code":  http.StatusBadRequest,
            "msg": err.Error(),
        })
        return
    }

    // 通过用户名获取用户 ID
    id, code := model.GetUserIDByUsername(username)
    if code != errmsg.SUCCESS {
        c.JSON(http.StatusInternalServerError, gin.H{
            "code":  http.StatusInternalServerError,
            "msg": errmsg.GetErrMsg(code),
        })
        return
    }

    // 转换文章 ID
    commentID, err := strconv.Atoi(cid)
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "code":  http.StatusBadRequest,
            "msg": "无效的评论 ID",
        })
        return
    }

    // 切换点赞状态
    code = model.FirstCommentToggleLike(uint(commentID), id)
    if code == errmsg.SUCCESS {
        c.JSON(http.StatusOK, gin.H{
            "code":  http.StatusOK,
            "msg": "操作成功",
            "data": nil,
        })
    } else {
        c.JSON(http.StatusInternalServerError, gin.H{
            "code":  http.StatusInternalServerError,
            "msg": errmsg.GetErrMsg(code),
        })
    }
}